package cn.imvc.app.controllers.goods;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsLimitTime;
import cn.imvc.app.models.GoodsLimitTimeModel;
import cn.imvc.app.models.GoodsModel;
import cn.imvc.app.models.LoggingModel;
import cn.imvc.core.DialogBean;
import cn.imvc.core.Render;
import cn.imvc.data.domain.ViewRequest;

public class GoodsLimitTimeControllerImpl implements GoodsLimitTimeController{
	static Logger logger = Logger.getLogger(GoodsLimitTimeControllerImpl.class);
	public ViewRequest viewRequest;
	public Page<GoodsLimitTime> page;
	public int id;
	public GoodsLimitTime goodsLimitTime;
	public Goods goods;
	@Autowired
	GoodsLimitTimeModel goodsLimitTimeModel;
	@Autowired
	GoodsModel goodsModel;
	@Autowired
	LoggingModel loggingModel;
	public String search() {
		return null;
	}

	public String index() {
		if(viewRequest==null){
		viewRequest = new ViewRequest();
		}
		page = goodsLimitTimeModel.find(viewRequest);
		return "goods-limit-time/index";
	}

	public String show() {
		goodsLimitTime = goodsLimitTimeModel.get(id);
		return "goods-limit-time/edit";
	}

	public String editNew() {
		return "goods-limit-time/edit";
	}

	public String create() {
		goodsLimitTime.setGoods(goodsModel.get(goods.getId()));
		int result = goodsLimitTimeModel.save(goodsLimitTime);
		if(result==-1){
			return Render.json(new DialogBean("300","此商品已加入限时购活动,不能重复添加!","","closeCurrent"));
		}
		loggingModel.info(logger,"添加限时购："+goodsLimitTime.getGoods());
		return Render.json(new DialogBean("200","创建成功","","closeCurrent"));
	}

	public String edit() {
		goodsLimitTime = goodsLimitTimeModel.get(id);
		goods = goodsLimitTime.getGoods();
		return "goods-limit-time/edit";
	}

	public String update() {
		goodsLimitTime.setGoods(goodsModel.get(goods.getId()));
		int result = goodsLimitTimeModel.update(id,goodsLimitTime);
		if(result==-1){
			return Render.json(new DialogBean("300","此商品已加入限时购活动,不能重复添加!","","closeCurrent"));
		}
		loggingModel.info(logger,"修改限时购："+goodsLimitTime.getGoods());
		return Render.json(new DialogBean("200","修改成功","","closeCurrent"));
	}

	public String destroy() {
		goodsLimitTimeModel.delete(id);
		loggingModel.info(logger,"删除限时购："+id);
		return Render.json(new DialogBean("200","删除成功","closeCurrent"));
	}

	public String destroyBatch() {
		return null;
	}

	public ViewRequest getViewRequest() {
		return viewRequest;
	}

	public void setViewRequest(ViewRequest viewRequest) {
		this.viewRequest = viewRequest;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public GoodsLimitTime getGoodsLimitTime() {
		return goodsLimitTime;
	}

	public void setGoodsLimitTime(GoodsLimitTime goodsLimitTime) {
		this.goodsLimitTime = goodsLimitTime;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

}
